# _*_ coding utf-8 _*_
# FILENAME：downloadHandleLogWindow.py
# DESCRIPTION: 上传处理日志窗体
# AUTH: jarvis zhang
# DATE: 2022/6/14 11:53 上午
from PyQt5.Qt import *
from PyQt5.QtGui import QStandardItem
from PyQt5.QtWidgets import QMessageBox
from sqlalchemy import desc

from components.BaseDetailWindow import BaseDetailWindow
from models.asyncExecLogModel import AsyncDownloadLogModel
from models.baseModel import get_session, delete
from utils import timeUtils, commonUtils


class DownloadHandleLogWindow(BaseDetailWindow):

    __type = ''

    def __init__(self, type):
        super(DownloadHandleLogWindow, self).__init__()
        self.resize(900, 300)
        self.__init_ui()
        self.__type = type

    ''' UI '''

    def __init_ui(self):
        print('init ui')
        # 信号槽连接
        # self.btn_add.clicked.connect(self.add_click)
        # self.btn_close.clicked.connect(self.close)

    ''' Methods '''

    #  窗体show
    def show(self):
        super(DownloadHandleLogWindow, self).show()
        self.setWindowTitle("下载日志管理")
        # 设置表头
        self.setTableViewHeaders()
        # 查询
        total = self.query_total()
        self.setTotalRecordCount(total)  # 总量
        self.recordQuery(0)  # page

        # 关闭窗体,释放资源

    def close(self):
        super(DownloadHandleLogWindow, self).close()

    def setTableViewHeaders(self):
        self.tableeHeaderLabels = ['编号', '文件名', '状态', '结果', '开始时间', '结束时间', '版本编号', '耗时']
        self.dataModel.setHorizontalHeaderLabels(self.tableeHeaderLabels)
        # 设置列宽
        self.tableView.setColumnWidth(0, 50)
        self.tableView.setColumnWidth(1, 200)
        self.tableView.setColumnWidth(2, 80)
        self.tableView.setColumnWidth(3, 150)
        self.tableView.setColumnWidth(4, 150)
        self.tableView.setColumnWidth(5, 150)
        self.tableView.setColumnWidth(6, 100)
        self.tableView.setColumnWidth(7, 80)
        #  隐藏列
        self.tableView.setColumnHidden(6, True)

    # 记录查询，重写
    def recordQuery(self, limitIndex):
        data = self.page(limitIndex)
        self.dataModel.clear()
        if len(data) > 0:
            for i in range(len(data)):
                # 添加
                newItem = QStandardItem(str(data[i].id))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 0, newItem)

                newItem = QStandardItem(str(data[i].file_name))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 1, newItem)

                statusStr = ''
                status = data[i].status
                if status == 1:
                    statusStr = '成功'
                elif status == 2:
                    statusStr = '失败'
                else:
                    statusStr = '进行中'
                newItem = QStandardItem(statusStr)
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 2, newItem)

                newItem = QStandardItem(commonUtils.xstr(data[i].reason))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 3, newItem)

                newItem = QStandardItem(commonUtils.xstr(data[i].start_time))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 4, newItem)

                newItem = QStandardItem(commonUtils.xstr(data[i].end_time))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 5, newItem)

                newItem = QStandardItem(str(data[i].version_id))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 6, newItem)

                newItem = QStandardItem(timeUtils.timedelta(str(data[i].start_time), str(data[i].end_time)))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.dataModel.setItem(i, 7, newItem)
        self.setTableViewHeaders()

    # 分页查询列表
    def page(self, limitIndex=0):
        # 获取dbsession
        session = get_session()
        # 拼接查询
        my_query = session.query(AsyncDownloadLogModel)
        my_query = my_query.filter(AsyncDownloadLogModel.is_del == 0)
        my_query = my_query.filter(AsyncDownloadLogModel.type == self.__type)
        # 拼接排序条件
        my_query = my_query.order_by(desc(AsyncDownloadLogModel.update_date))
        # 分页
        my_query = my_query.offset(limitIndex).limit(self.PageRecordCount)
        # 执行查询
        model = my_query.all()
        session.close()
        return model

    # 查询数据总量
    def query_total(self):
        # 获取dbsession
        session = get_session()
        # 拼接查询
        my_query = session.query(AsyncDownloadLogModel)
        my_query = my_query.filter(AsyncDownloadLogModel.is_del == 0)
        my_query = my_query.filter(AsyncDownloadLogModel.type == self.__type)
        # 执行查询
        count = my_query.count()
        session.close()
        return count

    # 新增
    def add_click(self):
        print("add")
        # VersionDialog.get_dialog(self)
